import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TableExample {
  public String baseURL = "http://accessibility.psu.edu/tablecomplexhtml";
  public WebDriver driver = new FirefoxDriver();
 
  @BeforeMethod
  public void setup()
  {
   driver.get(baseURL);
  }
 
  @AfterMethod
  public void tear()
  {
   driver.quit();
  }
 
  @Test
  public void testTableExample()
  {
   List<WebElement> rows = driver.findElements(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr")); // Returns the number of Rows
   List<WebElement> cols = driver.findElements(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr[1]/td")); // Returns the number of Columns
  
   // Static XPath of a single TR and TD is ".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr[1]/td[1]"
  
   // code to fetch each and every value of a Table
  
   for(int rowNumber = 1; rowNumber <= rows.size(); rowNumber++)
   {
    for(int colNumber = 1; colNumber <= cols.size(); colNumber++)
    {
     System.out.print(driver.findElement(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr["+rowNumber+"]/td["+colNumber+"]")).getText()+"  ");
    }
    System.out.println("\n");
   }
  
   System.out.println("###############################################################################\n");

  // nth element value of a table 
  
   System.out.println(driver.findElement(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr["+rows.size()+"]/td["+cols.size()+"]")).getText());
  
  }
}